package com.sgkusbcamdemo;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.gkusbsdk.SZUSBCommand;
import com.gkusbsdk.SZUSBFile;
import com.gkusbsdk.SZUSBIFrame;
import com.qitianyong.selfclass.BUS_MESSAGE;
import com.qitianyong.selfclass.CONSTANT;
import com.qitianyong.selfclass.GKDevice;
import com.qitianyong.selfclass.LogUtils;
import com.qitianyong.selfclass.Message2EventBus;

import de.greenrobot.event.EventBus;

/**
 * Created by gy on 2017/4/24.
 */

public class CaptureAndGetDataDialog extends AlertDialog  {

    private Button capture;
    private TextView content;


    protected CaptureAndGetDataDialog(Context context) {
        super(context);
        initView();
    }

    private void initView() {
        View inflate = View.inflate(getContext(), R.layout.layout_cature, null);
        setView(inflate);
        capture = (Button)inflate.findViewById(R.id.capture);
        content = (TextView)inflate.findViewById(R.id.content);
        capture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SZUSBFile usbFile = new SZUSBFile();
                usbFile.__lock = SZUSBCommand.FILE_TYPE.PHOTO_FILE;
                usbFile.__command = SZUSBCommand.PHOTO_FILE_CONTROL.CONTROL_CAPTURE;
                int ret = GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_FILE_CONTROL, usbFile);
               if (ret != -1) {
                   capture.setEnabled(false);
                   capture.setText("。。。");
                }
            }
        });
    }

    @Override
    public void show() {
        super.show();
        EventBus.getDefault().register(this);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        EventBus.getDefault().unregister(this);
    }
    public void onEventMainThread(Message2EventBus message) {

        switch (message.__what) {
            case BUS_MESSAGE.BUS_SMALL_PHOTO_ERR:
                LogUtils.i("System","获取失败");
                capture.setEnabled(true);
                capture.setText("拍照");
                break;
            case BUS_MESSAGE.BUS_SMALL_PHOTO: {
                SZUSBIFrame object = (SZUSBIFrame) message.object;
                byte[] bytes = object.__iFrame;
                if (bytes==null||bytes.length==0) {
                    LogUtils.i("System","获取失败");
                } else {
                    LogUtils.i("System","获取成功"+bytes.length);
                    content.setText("name="+new String(object.__name)+" size = "+bytes.length);
                }
                capture.setEnabled(true);
                capture.setText("拍照");
                break;
            }
            case BUS_MESSAGE.BUS_CONTROL_OK: {
                SZUSBFile usbFile = (SZUSBFile) message.object;
                if (usbFile.__lock == SZUSBCommand.FILE_TYPE.PHOTO_FILE) {
                    switch (usbFile.__command) {
                        case SZUSBCommand.PHOTO_FILE_CONTROL.CONTROL_CAPTURE: {
                            GKDevice.getInstance().sendCommand( CONSTANT.COMMAND.CMD_GET_PHOTO, usbFile);
                            break;
                        }
                    }
                }
                break;
            }
            default:
                break;
        }
    }

}
